<!DOCTYPE html><html lang="en"><head><title>ExecutePayment</title></head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0"><meta name="groc-relative-root" content=""><meta name="groc-document-path" content="ExecutePayment"><meta name="groc-project-path" content="ExecutePayment.php"><link rel="stylesheet" type="text/css" media="all" href="assets/style.css"><script type="text/javascript" src="assets/behavior.js"></script><body><div id="meta"><div class="file-path">ExecutePayment.php</div></div><div id="document"><div class="segment"><div class="code"><div class="wrapper"><span class="cp">&lt;?php</span></div></div></div><div class="segment"><div class="comments"><div class="wrapper"><h1 id="execute-payment-sample">Execute Payment Sample</h1>

<p>This sample shows how you can complete
a payment that has been approved by
the buyer by logging into paypal site.
You can optionally update transaction
information by passing in one or more transactions.
API used: POST '/v1/payments/payment/<payment-id>/execute'.</p></div></div><div class="code"><div class="wrapper"><span class="k">require</span> <span class="nx">__DIR__</span> <span class="o">.</span> <span class="s1">&#39;/../bootstrap.php&#39;</span><span class="p">;</span>
<span class="k">use</span> <span class="nx">PayPal\Api\ExecutePayment</span><span class="p">;</span>
<span class="k">use</span> <span class="nx">PayPal\Api\Payment</span><span class="p">;</span>
<span class="k">use</span> <span class="nx">PayPal\Api\PaymentExecution</span><span class="p">;</span>
<span class="nb">session_start</span><span class="p">();</span>
<span class="k">if</span><span class="p">(</span><span class="nb">isset</span><span class="p">(</span><span class="nv">$_GET</span><span class="p">[</span><span class="s1">&#39;success&#39;</span><span class="p">])</span> <span class="o">&amp;&amp;</span> <span class="nv">$_GET</span><span class="p">[</span><span class="s1">&#39;success&#39;</span><span class="p">]</span> <span class="o">==</span> <span class="s1">&#39;true&#39;</span><span class="p">)</span> <span class="p">{</span>
  </div></div></div><div class="segment"><div class="comments"><div class="wrapper"><p>Get the payment Object by passing paymentId
payment id was previously stored in session in
CreatePaymentUsingPayPal.php</p></div></div><div class="code"><div class="wrapper">  <span class="nv">$paymentId</span> <span class="o">=</span> <span class="nv">$_SESSION</span><span class="p">[</span><span class="s1">&#39;paymentId&#39;</span><span class="p">];</span>
  <span class="nv">$payment</span> <span class="o">=</span> <span class="nx">Payment</span><span class="o">::</span><span class="na">get</span><span class="p">(</span><span class="nv">$paymentId</span><span class="p">,</span> <span class="nv">$apiContext</span><span class="p">);</span>
  </div></div></div><div class="segment"><div class="comments"><div class="wrapper"><p>PaymentExecution object includes information necessary 
to execute a PayPal account payment. 
The payer_id is added to the request query parameters
when the user is redirected from paypal back to your site</p></div></div><div class="code"><div class="wrapper">  <span class="nv">$execution</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">PaymentExecution</span><span class="p">();</span>
  <span class="nv">$execution</span><span class="o">-&gt;</span><span class="na">setPayerId</span><span class="p">(</span><span class="nv">$_GET</span><span class="p">[</span><span class="s1">&#39;PayerID&#39;</span><span class="p">]);</span>
  
  <span class="c1">//Execute the payment</span></div></div></div><div class="segment"><div class="comments"><div class="wrapper"><p>(See bootstrap.php for more on <code>ApiContext</code>)</p></div></div><div class="code"><div class="wrapper">  <span class="nv">$payment</span><span class="o">-&gt;</span><span class="na">execute</span><span class="p">(</span><span class="nv">$execution</span><span class="p">,</span> <span class="nv">$apiContext</span><span class="p">);</span>

  <span class="k">echo</span> <span class="s2">&quot;&lt;html&gt;&lt;body&gt;&lt;pre&gt;&quot;</span><span class="p">;</span>
  <span class="nb">var_dump</span><span class="p">(</span><span class="nv">$payment</span><span class="o">-&gt;</span><span class="na">toArray</span><span class="p">());</span>
  <span class="k">echo</span> <span class="s2">&quot;&lt;/pre&gt;&lt;a href=&#39;../index.html&#39;&gt;Back&lt;/a&gt;&lt;/body&gt;&lt;/html&gt;&quot;</span><span class="p">;</span>
  
<span class="p">}</span> <span class="k">else</span> <span class="p">{</span>
  <span class="k">echo</span> <span class="s2">&quot;User cancelled payment.&quot;</span><span class="p">;</span>
<span class="p">}</span></div></div></div></div></body></html>